﻿package com.skillscoach  {
	import alducente.services.WebService;
	
	import com.adobe.images.JPGEncoder;
	import com.skillscoach.ToolsPanel;
	
	import fl.transitions.Tween;
	import fl.transitions.easing.Strong;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.GradientType;
	import flash.display.Graphics;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.MovieClip;
	import flash.display.SpreadMethod;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.ProgressEvent;
	import flash.events.StatusEvent;
	import flash.events.TimerEvent;
	import flash.external.ExternalInterface;
	import flash.geom.Matrix;
	import flash.geom.Rectangle;
	import flash.media.Microphone;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;
	import flash.net.navigateToURL;
	import flash.system.Security;
	import flash.utils.ByteArray;
	import flash.utils.Timer;
	
	import org.bytearray.micrecorder.MicRecorder;
	import org.bytearray.micrecorder.encoder.WaveEncoder;
	import org.bytearray.micrecorder.events.RecordingEvent;
	import flash.media.SoundTransform;
	import flash.media.SoundChannel;
	
	
	
	
	public class Config extends MovieClip{
		
		public var stageClip:MovieClip = this as MovieClip;
		public var toolsPanel:ToolsPanel;
		public var leftVideoHolder:MovieClip;
		public var rightVideoHolder:MovieClip;
		public var videoPlayPause:MovieClip;
		public var videoSeekbarLeft:Seekbar;
		public var videoSeekbarRight:Seekbar;
		public var videoSeekbarPanel:MovieClip;
		public var bottomControlPanel:MovieClip;
		public var myChannel:SoundChannel = new SoundChannel();
		
		
		
		public var noOfSavedThumbsLeft:uint = 0;
		public var noOfThumbsMovedLeft:uint = 0;
		public var noOfThumbsLoadedLeft:int = 0;
		
		public var noOfSavedThumbsRight:uint = 0;
		public var noOfThumbsMovedRight:uint = 0;
		public var noOfThumbsLoadedRight:int = 0;
		public var thumbsInitPos:uint = 25;
		
		private var ws:WebService;
		private var mic:Microphone;
		private var recorder:MicRecorder;
		private var waveEncoder:WaveEncoder = new WaveEncoder();
		
		private var fps:int = 24;
		private var speed:Number = 1;
		private var playerTimer:Timer = new Timer((1000/fps)/speed);
		
		//-- For Configuration
		public var xmlLoader:URLLoader;
		public var leftConfigXML:XML;
		public var rightConfigXML:XML;
		private var curTime:Date;
		public var leftConfigXMLURL:String;
		public var rightConfigXMLURL:String;
		//public var leftConfigXMLURL:String = "http://golfcoachlink.com/VideoAnalysis/11478_Analysis.xml?random="+getCurrentTime();
		//public var rightConfigXMLURL:String = "http://golfcoachlink.com/VideoAnalysis/11478_Analysis.xml?random="+getCurrentTime();
		
		
		//---- Video Details
		private var leftVideo:VideoObject = new VideoObject();
		private var rightVideo:VideoObject = new VideoObject();
		
		private var soundBytes:ByteArray;
		private var paramObj:Object;
		private var deleteFrameNo:Number;	
		private var deleteFrameId:String;
		private var debug:Boolean = false;	
		private var currentVideoLoading:String;
		      
		
		public function Config() {
			Security.allowDomain("*")
			this.addEventListener(Event.ENTER_FRAME,enterFrame);
			
			
			
			
		}
		
		private function enterFrame(e:Event)
		{
			var per:Number = Math.round((this.loaderInfo.bytesLoaded/this.loaderInfo.bytesTotal)*100)
			if(per == 100)
			{
				
				this.removeEventListener(Event.ENTER_FRAME,enterFrame);
				gotoAndStop(2);
				infoMc.visible = false;	
				waitMc.visible = false;	
				deleteConfirmMc.visible = false;
			
				loadLeftXml();
			}			 
		}
		private function loadLeftXml():void {
			leftVideoHolder = leftVideoPlayerComp;
			rightVideoHolder = rightVideoPlayerComp;
			paramObj = this.loaderInfo.parameters;
			xmlLoader = new URLLoader();
			//if(!debug)
				//change here
			
			
			if(paramObj.leftXMLFilePath == "" || paramObj.leftXMLFilePath == undefined || paramObj.leftXMLFilePath == "undefined")
			{
				
				//waitMc.visible = true;
				//waitMc.loadTxt.text = "Please select Left Video";
				leftConfigXMLURL = "http://www.golfcoachlink.com/VideoAnalysis/11478_Analysis.xml?random="+getCurrentTime()
				xmlLoader.addEventListener(Event.COMPLETE, onLeftXMLLoadComplete);
				xmlLoader.load(new URLRequest(leftConfigXMLURL));
			}
			else {
				waitMc.visible = false;
				leftConfigXMLURL = String(paramObj.leftXMLFilePath+"?random="+getCurrentTime());
				xmlLoader.addEventListener(Event.COMPLETE, onLeftXMLLoadComplete);
				xmlLoader.load(new URLRequest(leftConfigXMLURL));
			}
			
				
				
			xmlLoader.addEventListener(Event.COMPLETE, onLeftXMLLoadComplete);
			xmlLoader.load(new URLRequest(leftConfigXMLURL));
		}
			
		
		private function onLeftXMLLoadComplete(evt:Event):void {
			
			
			leftConfigXML = new XML(evt.target.data);
			
			leftVideo.videoID = leftConfigXML.video.videoInfo.id;
			leftVideo.videoName = leftConfigXML.video.videoInfo.name;
			leftVideo.videoOwner = leftConfigXML.video.videoInfo.owner;
			leftVideo.videoDescription = leftConfigXML.video.videoInfo.description;
			leftVideo.videoURL = leftConfigXML.video.videoInfo.videoURL;
			leftVideo.videoAddedDate = leftConfigXML.video.videoInfo.dateAdded;
			leftVideo.videoDuration = leftConfigXML.video.videoInfo.duration;
			
			
			
			
			var imgArray:XMLList = leftConfigXML.video.videoEditInfo.savedAnalysis.analyse;
			for(var i:int = 0;i< imgArray.length();i++)
			{
			
				leftVideo.dataObj.setInfo(imgArray[i]);				
			}
			leftVideo.dataObj.waitMc = this.waitMc;
			loadVideoLeft(leftVideo.videoURL);
			loadRightXml();
			
		}
		
		private function loadRightXml():void {
			
			xmlLoader = new URLLoader();
			//if(!debug)
				//change here
						
			if(paramObj.rightXMLFilePath == "" || paramObj.rightXMLFilePath == undefined || paramObj.rightXMLFilePath == "undefined")
			{
				//waitMc.visible = true;
				//waitMc.loadTxt.text = "Please select Right Video";
				rightConfigXMLURL = "http://www.golfcoachlink.com/VideoAnalysis/11477_Analysis.xml?random="+getCurrentTime();
				xmlLoader.addEventListener(Event.COMPLETE, onRightXMLLoadComplete);
				xmlLoader.load(new URLRequest(rightConfigXMLURL));
				
				
			}
			else {
				waitMc.visible = false;
				rightConfigXMLURL = String(paramObj.rightXMLFilePath+"?random="+getCurrentTime());
				xmlLoader.addEventListener(Event.COMPLETE, onRightXMLLoadComplete);
				xmlLoader.load(new URLRequest(rightConfigXMLURL));
			}
			
				
			xmlLoader.addEventListener(Event.COMPLETE, onRightXMLLoadComplete);
			xmlLoader.load(new URLRequest(rightConfigXMLURL));
		}
			
		
		private function onRightXMLLoadComplete(evt:Event):void {
			
			
			rightConfigXML = new XML(evt.target.data);
			
			rightVideo.videoID = rightConfigXML.video.videoInfo.id;
			rightVideo.videoName = rightConfigXML.video.videoInfo.name;
			rightVideo.videoOwner = rightConfigXML.video.videoInfo.owner;
			rightVideo.videoDescription = rightConfigXML.video.videoInfo.description;
			rightVideo.videoURL = rightConfigXML.video.videoInfo.videoURL;
			rightVideo.videoAddedDate = rightConfigXML.video.videoInfo.dateAdded;
			rightVideo.videoDuration = rightConfigXML.video.videoInfo.duration;
			
			
			trace(rightConfigXML)
			
			var imgArray:XMLList = rightConfigXML.video.videoEditInfo.savedAnalysis.analyse;
			for(var i:int = 0;i< imgArray.length();i++)
			{
			
				rightVideo.dataObj.setInfo(imgArray[i]);				
			}
			rightVideo.dataObj.waitMc = this.waitMc;
			loadVideoRight(rightVideo.videoURL);
			allXMLLoaded();
			
		}
		
		private function allXMLLoaded():void
		{
			
			
			pleasewaitMc.visible = false;	
			deleteConfirmMc.visible = false;	
			deleteConfirmMc.yesBtn.addEventListener(MouseEvent.CLICK,deleteFrame);
			deleteConfirmMc.noBtn.addEventListener(MouseEvent.CLICK,cancelDeleteFrame);
			infoMc.txt.text = " Id : "+leftVideo.videoID +" \n Name : "+leftVideo.videoName+" \n Owner : "+leftVideo.videoOwner +" \n Description : "+leftVideo.videoDescription+" \n Added Date : "+leftVideo.videoAddedDate +" \n Duration : "+leftVideo.videoDuration;
			infoMc.okBtn.addEventListener(MouseEvent.CLICK,hideInfo);
			
			infoMc.visible = false;	
			waitMc.visible = false;	
			intScreen();	
		}
		private function showInfo(e:MouseEvent):void
		{
			infoMc.visible = true;	
		}
		private function hideInfo(e:MouseEvent):void
		{
			infoMc.visible = false;	
		}
		private function intScreen():void
		{
			
			videoSeekbarPanel = stageClip.getChildByName("videoSeekbarPanel") as MovieClip;
			bottomControlPanel = stageClip.getChildByName("bottomControlPanel") as MovieClip;
			videoPlayPause = bottomControlPanel.getChildByName("videoPlayBtn") as MovieClip;	
			
			//Add Tools Panel
			
			
			//Add Seekbar Panel Left
			videoSeekbarLeft = new Seekbar();
			videoSeekbarPanel.addChild(videoSeekbarLeft);
			videoSeekbarLeft.x = 15;
			videoSeekbarLeft.y = 15;
			
			//Add Seekbar Panel Left
			videoSeekbarRight = new Seekbar();
			videoSeekbarPanel.addChild(videoSeekbarRight);
			videoSeekbarRight.x = 580;
			videoSeekbarRight.y = 15;
			

			
			
			ws = new WebService();
			ws.addEventListener(Event.CONNECT, wsdlConnected);
			//change here
			if(paramObj.ServicePath == "" || paramObj.ServicePath == undefined || paramObj.ServicePath == "undefined"){
				ws.connect("http://www.golfcoachlink.com/Services/VideoAnalysis.asmx?WSDL");
			}
				
				else {
				ws.connect(String(paramObj.ServicePath)+"?WSDL");
				}
				

			


			
			bottomControlPanel.mouseEnabled = false;
			bottomControlPanel.mouseChildren= false;
			bottomControlPanel.alpha = .5;
			
			
			
		}
		
		
		private function wsdlConnected(evt:Event):void{
			
			//loadVideo();
			
		}
		private function loadVideoLeft(path:String):void
		{
			//change here
			var url:URLRequest = new URLRequest(path);
			
			//var url:URLRequest = new URLRequest("http://testgic.onlineskillscoach.com/VideoAnalysis/AnalysisVideo/mexican.swf");
			var videoLoader:Loader = new Loader();
			videoLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,onVideoLoadLeft);
			videoLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progressListener);		
			videoLoader.load(url);	
			
			
		}
		private function loadVideoRight(path:String):void
		{
			//change here
			var url:URLRequest = new URLRequest(path);
			//var url:URLRequest = new URLRequest("http://testgic.onlineskillscoach.com/VideoAnalysis/AnalysisVideo/mexican.swf");
			var videoLoader:Loader = new Loader();
			videoLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,onVideoLoadRight);
			videoLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progressListener);		
			videoLoader.load(url);	
			
			
		}
		function progressListener (e:ProgressEvent):void{
			try{
				MovieClip(e.target.content).stop();
			}catch(e){
				
			}
			//trace("Downloaded " + e.bytesLoaded + " out of " + e.bytesTotal + " bytes of video");
		}
		private function onVideoLoadLeft(event:Event):void
		{
			
			var videoClip:MovieClip = (event.target.content) as MovieClip;
			videoClip.x = -videoClip.width/2;
			videoClip.y = -videoClip.height/2;
			
			
			leftVideoHolder.videoClipHolder.addChild(videoClip);
				
			noOfSavedThumbsLeft = leftVideo.dataObj.frameArray.length;
			noOfThumbsLoadedLeft = -1;
			loadImageLeft(null);	
			leftVideoHolder.videoClipHolder.getChildAt(0).gotoAndStop(1);	
			var mSound:SoundTransform = new SoundTransform(2);
			leftVideoHolder.videoClipHolder.soundTransform = mSound;
			
			
			
			
		}
		
		private function onVideoLoadRight(event:Event):void
		{
			var videoClip:MovieClip = (event.target.content) as MovieClip;
			videoClip.x = -videoClip.width/2;
			videoClip.y = -videoClip.height/2;
			rightVideoHolder.videoClipHolder.addChild(videoClip);
				noOfSavedThumbsRight = rightVideo.dataObj.frameArray.length;
				noOfThumbsLoadedRight = -1;
				loadImageRight(null);	
				
				rightVideoHolder.videoClipHolder.getChildAt(0).gotoAndStop(1);
				var mSound2:SoundTransform = new SoundTransform(2);
			rightVideoHolder.videoClipHolder.soundTransform = mSound2;
		}
		private function loadImageLeft(e:Event):void
		{	
			if(noOfThumbsLoadedLeft > -1)
			{
				var info:EditInfoObject = leftVideo.dataObj.getInfoByCuePoint(leftVideo.dataObj.frameArray[noOfThumbsLoadedLeft]) as EditInfoObject;	
				info.image = LoaderInfo(e.target).content as Bitmap;
				info.image.x = -info.image.width/2;
				info.image.y = -info.image.height/2;
				if(info.cuePoint != 1)
					info.image.visible = false;
				leftVideoHolder.addChild(info.image);
			}
			
			
			noOfThumbsLoadedLeft++;
			
			if(noOfThumbsLoadedLeft < noOfSavedThumbsLeft)
			{
				info = leftVideo.dataObj.getInfoByCuePoint(leftVideo.dataObj.frameArray[noOfThumbsLoadedLeft]) as EditInfoObject;	
				var urlRequest:URLRequest = new URLRequest(info.imageUrl);
				var imageLoader:Loader = new Loader();
				
				imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,loadImageLeft);
				imageLoader.load(urlRequest);	
				
			}else {
				//onAllImageLoad();
			}
			
		}
		
		private function loadImageRight(e:Event):void
		{	
			if(noOfThumbsLoadedRight > -1)
			{
				var info:EditInfoObject = rightVideo.dataObj.getInfoByCuePoint(rightVideo.dataObj.frameArray[noOfThumbsLoadedRight]) as EditInfoObject;	
				info.image = LoaderInfo(e.target).content as Bitmap;
				info.image.x = -info.image.width/2;
				info.image.y = -info.image.height/2;
				if(info.cuePoint != 1)
					info.image.visible = false;
				rightVideoHolder.addChild(info.image);
			}
			
			
			noOfThumbsLoadedRight++;
			
			if(noOfThumbsLoadedRight < noOfSavedThumbsRight)
			{
				info = rightVideo.dataObj.getInfoByCuePoint(rightVideo.dataObj.frameArray[noOfThumbsLoadedRight]) as EditInfoObject;	
				var urlRequest:URLRequest = new URLRequest(info.imageUrl);
				var imageLoader:Loader = new Loader();
				
				imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,loadImageRight);
				imageLoader.load(urlRequest);	
				
			}else {
				onAllImageLoad();
			}
			
		}
		
		private function onAllImageLoad():void
		{			
			
			bottomControlPanel.mouseEnabled = true;
			bottomControlPanel.mouseChildren= true;
			bottomControlPanel.alpha = 1;
			
			loadingVideoMc.visible = false;
			
			
			playerTimer.addEventListener(TimerEvent.TIMER,playingVideo);
			
			
			
			
			
			
			
			videoSeekbarLeft.dataObj = leftVideo.dataObj;
			videoSeekbarLeft.initControls(leftVideoHolder.videoClipHolder.getChildAt(0), videoPlayPause, playerTimer);
			
			videoSeekbarRight.dataObj = rightVideo.dataObj;
			videoSeekbarRight.initControls(rightVideoHolder.videoClipHolder.getChildAt(0), videoPlayPause, playerTimer);
			
			//---- Slow Motion
			bottomControlPanel.slowMotionBack.addEventListener(MouseEvent.CLICK, slowMotionBackHandler);
			bottomControlPanel.slowMotionFront.addEventListener(MouseEvent.CLICK, slowMotionFrontHandler);
			
			bottomControlPanel.frameByFrameFront.addEventListener(MouseEvent.CLICK, frameByFrameFrontHandler);
			bottomControlPanel.frameByFrameBack.addEventListener(MouseEvent.CLICK, frameByFrameBackHandler);
			
			//--- Saved thumbs Navigation
			savedFramesHolderLeft.thumbsNavBack.addEventListener(MouseEvent.CLICK, savedFramesThumbsMoveLeft);
			savedFramesHolderLeft.thumbsNavFront.addEventListener(MouseEvent.CLICK, savedFramesThumbsMoveLeft);
			
			savedFramesHolderRight.thumbsNavBack.addEventListener(MouseEvent.CLICK, savedFramesThumbsMoveRight);
			savedFramesHolderRight.thumbsNavFront.addEventListener(MouseEvent.CLICK, savedFramesThumbsMoveRight);
			
			
			//---- Analyse Button

			
			

			
			//flvPlayback.source = "http://o-o.preferred.bom03s03.v8.lscache5.c.youtube.com/videoplayback?sparams=id%2Cexpire%2Cip%2Cipbits%2Citag%2Csource%2Calgorithm%2Cburst%2Cfactor%2Ccp&fexp=913536%2C916800%2C905266&algorithm=throttle-factor&itag=34&ip=112.0.0.0&burst=40&sver=3&signature=496B42674A7EC62036B25E3AEAB99DD202F6BB3D.07698ABC20D3A4BD2AE67134C383E6527F92A67B&source=youtube&expire=1326709955&key=yt1&ipbits=8&factor=1.25&cp=U0hRS1dLV19KT0NOMV9PRUFKOlJEWGJ2emdXWXls&id=58603d1e87c4d449&title=Golf%20Pitch%20Shot.flv";
			videoPlayPause.addEventListener(MouseEvent.CLICK, playPauseVideo);
			videoPlayPause.addEventListener(MouseEvent.ROLL_OVER, playPauseOver);
			videoPlayPause.addEventListener(MouseEvent.ROLL_OUT, playPauseOut);
			
			videoPlayPause.mouseChildren = false;	
			videoPlayPause.buttonMode = true;
			

			
						
			generateThumbLeft();
			generateThumbRight();
			
		}
		private function generateThumbLeft():void {
			
			
			
				noOfSavedThumbsLeft = leftVideo.dataObj.frameArray.length;
				for (var i:int = 0; i < noOfSavedThumbsLeft; i++) 
				{
					var newThumbLeft:MovieClip = new Thumbnail();
					savedFramesHolderLeft.thumbnailStrip.addChild(newThumbLeft);
					newThumbLeft.name = "thumb"+i;
					newThumbLeft.x = i*(newThumbLeft.width+6);
		
					var infoLeft:EditInfoObject = leftVideo.dataObj.getInfoByCuePoint(leftVideo.dataObj.frameArray[i]) as EditInfoObject;
					var bitmap:Bitmap = new Bitmap(infoLeft.image.bitmapData);
					
					
					bitmap.width = 108;
					bitmap.height = 86;
					newThumbLeft.thumbnailHolder.addChild(bitmap);
				} 
		}
			
		
		private function generateThumbRight():void
		{
				noOfSavedThumbsRight = rightVideo.dataObj.frameArray.length;
				for (var j:int = 0; j < noOfSavedThumbsRight; j++) 
				{
				var newThumbRight:MovieClip = new Thumbnail();
				savedFramesHolderRight.thumbnailStrip.addChild(newThumbRight);
				newThumbRight.name = "thumb"+j;
				newThumbRight.x = j*(newThumbRight.width+6);
	
				var infoRight:EditInfoObject = rightVideo.dataObj.getInfoByCuePoint(rightVideo.dataObj.frameArray[j]) as EditInfoObject;
				var bitmapRight:Bitmap = new Bitmap(infoRight.image.bitmapData);
				
				
				bitmapRight.width = 108;
				bitmapRight.height = 86;
				newThumbRight.thumbnailHolder.addChild(bitmapRight);
				}
		}
		private function deleteFrame(e:MouseEvent):void {
			
			
			pleasewaitMc.visible = true;	
			ws.DeleteFrame(deleteFrameResponse,deleteFrameId,deleteFrameNo);
			
			if(e.target.viType == "left"){
				leftVideo.dataObj.deleteInfoByCuePoint(deleteFrameNo);
				generateThumbLeft();
			}else {
				rightVideo.dataObj.deleteInfoByCuePoint(deleteFrameNo);
				generateThumbRight();
			}
			
			
			deleteConfirmMc.visible = false;
			
		}
		
		private function cancelDeleteFrame(e:MouseEvent):void {
			deleteConfirmMc.visible = false;				
		}
		private function showDeleteFrame(e:MouseEvent):void {
			
			deleteFrameId = MovieClip(e.target).videoId;
			deleteFrameNo = MovieClip(e.target).cue;
			deleteConfirmMc.visible = true;				
		}
		private function deleteFrameResponse(serviceRespone:XML):void
		{
			pleasewaitMc.visible = false;	
			trace("Web Service Result: deleteFrameResponse");
			trace(serviceRespone);
		}
		private function playingVideo(e:TimerEvent):void {
		
			var vClipLeft:MovieClip = leftVideoHolder.videoClipHolder.getChildAt(0);
			var vClipRight:MovieClip = rightVideoHolder.videoClipHolder.getChildAt(0);
			
			leftVideo.dataObj.HideAllImages();
			rightVideo.dataObj.HideAllImages();
			//leftVideo.dataObj.HideImageByCuePoint(vClipLeft.currentFrame);
			//rightVideo.dataObj.HideImageByCuePoint(vClipRight.currentFrame);
			
			if(vClipLeft.currentFrame == vClipLeft.totalFrames)
			{
				vClipLeft.gotoAndStop(1)
			}else {
				//vClipLeft.play();
				//vClipLeft.nextFrame();
				
			}
			
			if(vClipRight.currentFrame == vClipRight.totalFrames)
			{
				vClipRight.gotoAndStop(1)
			}else {
				//vClipRight.play();
				//vClipRight.nextFrame();
				
			}
			
			
			for (var i:int = 0; i < leftVideo.dataObj.frameArray.length; i++) 
			{
				if(leftVideo.dataObj.frameArray[i] == vClipLeft.currentFrame)
				{
					leftVideo.dataObj.showImageByCuePoint(vClipLeft.currentFrame);
					vClipLeft.stop();
					vClipRight.stop();
					playerTimer.stop();
					videoPlayPause.gotoAndStop(1)
				}
			}
			
			
			for (var j:int = 0; j < rightVideo.dataObj.frameArray.length; j++) 
			{
				if(rightVideo.dataObj.frameArray[j] == vClipRight.currentFrame)
				{
					rightVideo.dataObj.showImageByCuePoint(vClipRight.currentFrame);
					vClipLeft.stop();
					vClipRight.stop();
					playerTimer.stop();
					videoPlayPause.gotoAndStop(1)
				}
			}
			
			
		}
	
		
		//------record sound---//
		private function micStatusHandler(evt:StatusEvent):void
		{
			switch (evt.code) {
				case "Microphone.Muted":
					//check youe mic 
				break;
			}
		}
		private function toggleRecord(e:MouseEvent):void {
			
			if(MovieClip(stageClip.recordBtn).currentFrame == 1)
			{
				recorder.record();		
				stageClip.recordBtn.gotoAndStop(2);
			}else {
				recorder.stop();
				stageClip.recordBtn.gotoAndStop(1);
			}
			//recordBaring....
		}
		private function recording(e:RecordingEvent):void {
			//recordBaring....
		}
		private function updateMeter(e:Event):void
		{
		}
		
		private function recordComplete(e:Event):void {

			soundBytes = (recorder.output);

		}
		
		private function savedFramesThumbsMoveLeft(evt:MouseEvent):void
		{
			trace(noOfSavedThumbsLeft, noOfThumbsMovedLeft);
			var strip:MovieClip = savedFramesHolderLeft.thumbnailStrip;
			if(evt.target.name == "thumbsNavBack" && noOfSavedThumbsLeft > 3 && noOfThumbsMovedLeft > 0)
			{
				noOfThumbsMovedLeft -= 1;
			}
			else if(evt.target.name == "thumbsNavFront" && noOfSavedThumbsLeft > 3 && (noOfSavedThumbsLeft-noOfThumbsMovedLeft) > 3)
			{
				noOfThumbsMovedLeft += 1;
			}
			
			var thumbTweenBack:Tween = new Tween(strip, "x", Strong.easeOut, strip.x,  25 - (118*noOfThumbsMovedLeft), 1, true);
		}
		private function savedFramesThumbsMoveRight(evt:MouseEvent):void
		{
			trace(noOfSavedThumbsRight, noOfThumbsMovedRight);
			var strip:MovieClip = savedFramesHolderRight.thumbnailStrip;
			if(evt.target.name == "thumbsNavBack" && noOfSavedThumbsRight > 3 && noOfThumbsMovedRight > 0)
			{
				noOfThumbsMovedRight -= 1;
			}
			else if(evt.target.name == "thumbsNavFront" && noOfSavedThumbsRight > 3 && (noOfSavedThumbsRight-noOfThumbsMovedRight) > 3)
			{
				noOfThumbsMovedRight += 1;
			}
			
			var thumbTweenBack:Tween = new Tween(strip, "x", Strong.easeOut, strip.x,  25 - (118*noOfThumbsMovedRight), 1, true);
		}
		private function saveFrame(evt:MouseEvent):void
		{
			saveFrameLeft();
			saveFrameRight();
		
		}
		
		private function saveFrameLeft():void
		{
			pleasewaitMc.visible = true;		
			
			var bounds:Rectangle = leftVideoPlayerComp.getBounds( leftVideoPlayerComp );
			var bd:BitmapData = new BitmapData( int( bounds.width + 0.5 ), int( bounds.height + 0.5 ), false, 0 );
			bd.draw( leftVideoPlayerComp, new Matrix(1,0,0,1,-bounds.x,-bounds.y) );			
			var encoder:JPGEncoder = new JPGEncoder(80);
			var bytes:ByteArray = encoder.encode(bd);
				
			try{
				recorder.stop();
				stageClip.recordBtn.gotoAndStop(1);
			}catch(e)
			{
				
			}
				
 			var item:XML = <analyse />;
			item.analysedUser = "Rajmohan";
			item.dateAnalysed = "2/9/2012 6:22:00 PM";
			item.cuePoint = Number(leftVideoHolder.videoClipHolder.getChildAt(0).currentFrame);
			item.savedFrame = null;
			leftVideo.dataObj.setInfo(item);	
			
			var info:EditInfoObject = leftVideo.dataObj.getInfoByCuePoint(item.cuePoint) as EditInfoObject;	

			try{
				leftVideoHolder.removeChild(info.image);	
			}catch(e){
				
			}
			info.image = new Bitmap(bd);
			info.image.x = leftVideoHolder.videoClipHolder.x;//-info.image.width/2;
			info.image.y = leftVideoHolder.videoClipHolder.y; //-info.image.height/2;
			leftVideoHolder.addChild(info.image);
			
			generateThumbLeft();
				
			saveFrameCliked();
			
			//bytes.writeUnsignedInt(bitmap.bitmapData.width);
			//bytes.writeBytes(bitmap.bitmapData.getPixels(bitmap.bitmapData.rect));
			//bytes.compress();
			if(soundBytes){
				ws.SaveFrame(saveFrameResponceLeft, leftVideo.videoID , String(leftVideoHolder.videoClipHolder.getChildAt(0).currentFrame),Base64.encode(bytes),Base64.encode(soundBytes),"2/9/2012 6:22PM","Rajmohan");
			}else {
				ws.SaveFrame(saveFrameResponceLeft, leftVideo.videoID , String(leftVideoHolder.videoClipHolder.getChildAt(0).currentFrame),Base64.encode(bytes),null,"2/9/2012 6:22PM","Rajmohan");
			}
		}

		private function saveFrameRight():void
		{
			pleasewaitMc.visible = true;		
			
			var bounds:Rectangle = rightVideoPlayerComp.getBounds( rightVideoPlayerComp );
			var bd:BitmapData = new BitmapData( int( bounds.width + 0.5 ), int( bounds.height + 0.5 ), false, 0 );
			bd.draw( rightVideoPlayerComp, new Matrix(1,0,0,1,-bounds.x,-bounds.y) );			
			var encoder:JPGEncoder = new JPGEncoder(80);
			var bytes:ByteArray = encoder.encode(bd);
				
			try{
				recorder.stop();
				stageClip.recordBtn.gotoAndStop(1);
			}catch(e)
			{
				
			}
				
 			var item:XML = <analyse />;
			item.analysedUser = "Rajmohan";
			item.dateAnalysed = "2/9/2012 6:22:00 PM";
			item.cuePoint = Number(rightVideoHolder.videoClipHolder.getChildAt(0).currentFrame);
			item.savedFrame = null;
			rightVideo.dataObj.setInfo(item);	
			
			var info:EditInfoObject = rightVideo.dataObj.getInfoByCuePoint(item.cuePoint) as EditInfoObject;	

			try{
				rightVideoHolder.removeChild(info.image);	
			}catch(e){
				
			}
			info.image = new Bitmap(bd);
			info.image.x = rightVideoHolder.videoClipHolder.x;//-info.image.width/2;
			info.image.y = rightVideoHolder.videoClipHolder.y;
			rightVideoHolder.addChild(info.image);
			
			generateThumbRight();
				
			saveFrameCliked();
			
			//bytes.writeUnsignedInt(bitmap.bitmapData.width);
			//bytes.writeBytes(bitmap.bitmapData.getPixels(bitmap.bitmapData.rect));
			//bytes.compress();
			if(soundBytes){
				ws.SaveFrame(saveFrameResponceRight, rightVideo.videoID , String(rightVideoHolder.videoClipHolder.getChildAt(0).currentFrame),Base64.encode(bytes),Base64.encode(soundBytes),"2/9/2012 6:22PM","Rajmohan");
			}else {
				ws.SaveFrame(saveFrameResponceRight, rightVideo.videoID , String(rightVideoHolder.videoClipHolder.getChildAt(0).currentFrame),Base64.encode(bytes),null,"2/9/2012 6:22PM","Rajmohan");
			}
		}

			
		private function saveFrameResponceLeft(serviceRespone:XML):void{
			var str:String = String(serviceRespone.children().children().children());
			
			var info:EditInfoObject = leftVideo.dataObj.getInfoByCuePoint(leftVideoHolder.videoClipHolder.getChildAt(0).currentFrame) as EditInfoObject;
			info.soundUrl = str.substr(str.indexOf("|")+1);
			 pleasewaitMc.visible = false;
			 soundBytes = null;
			 leftVideoHolder.videoClipHolder.rotation = 0;
			 toolsPanel.clearFrameChild();	 
		}
		
		private function saveFrameResponceRight(serviceRespone:XML):void{
			var str:String = String(serviceRespone.children().children().children());
			
			var info:EditInfoObject = rightVideo.dataObj.getInfoByCuePoint(rightVideoHolder.videoClipHolder.getChildAt(0).currentFrame) as EditInfoObject;
			info.soundUrl = str.substr(str.indexOf("|")+1);
			 pleasewaitMc.visible = false;
			 soundBytes = null;
			 rightVideoHolder.videoClipHolder.rotation = 0;
			 toolsPanel.clearFrameChild();	 
		}
		private function loadSavedFrmae(url:String):void{
			
		}
		private function startAnalyse(evt:MouseEvent):void
		{
			toolsPanel.createChild();
			videoSeekbarPanel.saveFrameButton.mouseEnabled = true;
			videoSeekbarPanel.saveFrameButton.alpha = 1;
			stageClip.recordBtn.mouseEnabled = true;
			stageClip.recordBtn.alpha = 1;
			videoSeekbarPanel.analyseButton.mouseEnabled = false;
			videoSeekbarPanel.analyseButton.alpha = .5;
			
			bottomControlPanel.mouseEnabled = false;
			bottomControlPanel.mouseChildren = false;
			bottomControlPanel.alpha = .5;
			
			toolsPanel.mouseChildren = true;
			toolsPanel.alpha = 1;
			playerTimer.stop();
			leftVideoHolder.videoClipHolder.getChildAt(0).stop();
			rightVideoHolder.videoClipHolder.getChildAt(0).stop();
			
			trace("am here")
			
			videoSeekbarLeft.alpha = .5;
			videoSeekbarRight.alpha = .5;
			videoSeekbarLeft.mouseChildren = false;
			videoSeekbarRight.mouseChildren = false;
			
			videoPlayPause.alpha = .5;
			
			videoPlayPause.enabled = false;
			videoPlayPause.mouseEnabled = false;
			videoPlayPause.gotoAndStop(1);		
			
		}
		private function endAnalyse(evt:MouseEvent):void
		{
			pleasewaitMc.visible = true;
			ws.CompleteAnalysis(completeAnalysisResponse,leftVideo.videoID);
			ws.CompleteAnalysis(completeAnalysisResponse,rightVideo.videoID);
		}
		private function completeAnalysisResponse(serviceRespone:XML):void
		{
			pleasewaitMc.visible = false;
			var urlRequest:URLRequest = new URLRequest("");
			urlRequest.method = URLRequestMethod.GET;
			var variable:URLVariables = new URLVariables();
			variable.videoIdLeft = leftVideo.videoID;
			variable.videoIdRight = rightVideo.videoID;
			urlRequest.data = variable;
			navigateToURL(urlRequest,"_self");
		}
		private function saveFrameCliked():void
		{
			bottomControlPanel.mouseEnabled = true;
			bottomControlPanel.mouseChildren= true;
			bottomControlPanel.alpha = 1;
			
			videoSeekbarPanel.analyseButton.mouseEnabled = true;
			videoSeekbarPanel.analyseButton.alpha = 1;
			
			videoSeekbarPanel.saveFrameButton.mouseEnabled = false;
			videoSeekbarPanel.saveFrameButton.alpha = .5;
			stageClip.recordBtn.mouseEnabled = false;
			stageClip.recordBtn.alpha = .5;
			
			toolsPanel.mouseChildren = false;
			toolsPanel.alpha = .5;
			//flvPlayback.play();
			videoSeekbarLeft.mouseChildren = true;
			videoSeekbarLeft.alpha = 1;
			videoSeekbarRight.mouseChildren = true;
			videoSeekbarRight.alpha = 1;
			
			videoPlayPause.mouseEnabled = true;
			
			videoPlayPause.alpha = 1;
		}
		private function slowMotionBackHandler(evt:MouseEvent):void
		{
			if(speed > .25)
			{
				speed -= .25
			}
			playerTimer.stop();
			playerTimer = new Timer((1000/fps)/speed)
			playerTimer.addEventListener(TimerEvent.TIMER,ffVideo);
			speedDisplyMc.txt.text = "X "+speed;
			
			if(videoPlayPause.currentFrame == 2)
			{
				playerTimer.start();
				//leftVideoHolder.videoClipHolder.getChildAt(0).play();
				//rightVideoHolder.videoClipHolder.getChildAt(0).play()
			}		
			
		}
		private function slowMotionFrontHandler(evt:MouseEvent):void
		{
			if(speed < 2)
			{
				speed += .25
				playerTimer.stop();
				playerTimer = new Timer((1000/fps)/speed)
				playerTimer.addEventListener(TimerEvent.TIMER,ffVideo);
				speedDisplyMc.txt.text = "X "+speed;
				if(videoPlayPause.currentFrame == 2)
				{
					playerTimer.start();
					//leftVideoHolder.videoClipHolder.getChildAt(0).play();
					//rightVideoHolder.videoClipHolder.getChildAt(0).play();
					
					
				}
			}
		}
		
		private function ffVideo(evt:TimerEvent)
		{
			var vClipLeft:MovieClip = leftVideoHolder.videoClipHolder.getChildAt(0);
			var vClipRight:MovieClip = rightVideoHolder.videoClipHolder.getChildAt(0);
			
			leftVideo.dataObj.HideAllImages();
			rightVideo.dataObj.HideAllImages();
			
			
			if(vClipLeft.currentFrame == vClipLeft.totalFrames)
			{
				vClipLeft.gotoAndStop(1)
			}else {
				//vClipLeft.play();
				vClipLeft.nextFrame();
				
			}
			
			if(vClipRight.currentFrame == vClipRight.totalFrames)
			{
				vClipRight.gotoAndStop(1)
			}else {
				//vClipRight.play();
				vClipRight.nextFrame();
				
			}
			
			
			for (var i:int = 0; i < leftVideo.dataObj.frameArray.length; i++) 
			{
				if(leftVideo.dataObj.frameArray[i] == vClipLeft.currentFrame)
				{
					leftVideo.dataObj.showImageByCuePoint(vClipLeft.currentFrame);
					vClipLeft.stop();
					vClipRight.stop();
					playerTimer.stop();
					videoPlayPause.gotoAndStop(1)
				}
			}
			
			
			for (var j:int = 0; j < rightVideo.dataObj.frameArray.length; j++) 
			{
				if(rightVideo.dataObj.frameArray[j] == vClipRight.currentFrame)
				{
					rightVideo.dataObj.showImageByCuePoint(vClipRight.currentFrame);
					vClipLeft.stop();
					vClipRight.stop();
					playerTimer.stop();
					videoPlayPause.gotoAndStop(1)
				}
			}
		}
		
		private function frameByFrameFrontHandler(evt:MouseEvent):void
		{
			playerTimer.stop();
			videoPlayPause.gotoAndStop(1);
			
			
			//For Left
			leftVideo.dataObj.HideImageByCuePoint(leftVideoHolder.videoClipHolder.getChildAt(0).currentFrame);


			
			leftVideoHolder.videoClipHolder.getChildAt(0).nextFrame();
			
			for (var i:int = 0; i < leftVideo.dataObj.frameArray.length; i++) 
			{
				if(leftVideo.dataObj.frameArray[i] == leftVideoHolder.videoClipHolder.getChildAt(0).currentFrame)
				{
					leftVideo.dataObj.showImageByCuePoint(leftVideoHolder.videoClipHolder.getChildAt(0).currentFrame);
				}
			}
			
			
			
			//For Right
			
			rightVideo.dataObj.HideImageByCuePoint(rightVideoHolder.videoClipHolder.getChildAt(0).currentFrame);


			
			rightVideoHolder.videoClipHolder.getChildAt(0).nextFrame();
			
			for (var j:int = 0; j < rightVideo.dataObj.frameArray.length; j++) 
			{
				if(rightVideo.dataObj.frameArray[j] == rightVideoHolder.videoClipHolder.getChildAt(0).currentFrame)
				{
					rightVideo.dataObj.showImageByCuePoint(rightVideoHolder.videoClipHolder.getChildAt(0).currentFrame);
				}
			}
			
		}
		private function frameByFrameBackHandler(evt:MouseEvent):void
		{
			
			playerTimer.stop();
			videoPlayPause.gotoAndStop(1);
			
			
			//For Left
			leftVideo.dataObj.HideImageByCuePoint(leftVideoHolder.videoClipHolder.getChildAt(0).currentFrame);			
			
			
			leftVideoHolder.videoClipHolder.getChildAt(0).prevFrame();
			
			for (var i:int = 0; i < leftVideo.dataObj.frameArray.length; i++) 
			{
				if(leftVideo.dataObj.frameArray[i] == leftVideoHolder.videoClipHolder.getChildAt(0).currentFrame)
				{
					leftVideo.dataObj.showImageByCuePoint(leftVideoHolder.videoClipHolder.getChildAt(0).currentFrame);
				}
			}
			
			
			//For Left
			rightVideo.dataObj.HideImageByCuePoint(rightVideoHolder.videoClipHolder.getChildAt(0).currentFrame);			
			
			
			rightVideoHolder.videoClipHolder.getChildAt(0).prevFrame();
			
			for (var j:int = 0; j < rightVideo.dataObj.frameArray.length; j++) 
			{
				if(rightVideo.dataObj.frameArray[j] == rightVideoHolder.videoClipHolder.getChildAt(0).currentFrame)
				{
					rightVideo.dataObj.showImageByCuePoint(rightVideoHolder.videoClipHolder.getChildAt(0).currentFrame);
				}
			}
			
		}
		/*private function gotoFrame(evt:MouseEvent):void
		{
			dataObj.HideImageByCuePoint(videoClip.currentFrame);			
			
			playerTimer.stop();
			videoPlayPause.gotoAndStop(1)
			videoClip.gotoAndStop();
			
			for (var i:int = 0; i < dataObj.frameArray.length; i++) 
			{
				if(dataObj.frameArray[i] == videoClip.currentFrame)
				{
					dataObj.showImageByCuePoint(videoClip.currentFrame);
				}
			}
			
		}*/
		private function playPauseVideo(evt:MouseEvent):void
		{
			if(evt.target.currentFrame == 1)
			{
				evt.target.gotoAndStop(2);
				leftVideoHolder.videoClipHolder.getChildAt(0).play();
				rightVideoHolder.videoClipHolder.getChildAt(0).play()
				playerTimer.start();
			}
			else
			{
				evt.target.gotoAndStop(1);
				playerTimer.stop();
				leftVideoHolder.videoClipHolder.getChildAt(0).stop();
				rightVideoHolder.videoClipHolder.getChildAt(0).stop();
			}
		}
		
		private function playPauseOver(evt:MouseEvent):void
		{
			evt.target.alphaLayer.alpha = .3;
			
		}
		private function playPauseOut(evt:MouseEvent):void
		{
			evt.target.alphaLayer.alpha = 0;
			
		}
		private function getCurrentTime():String
		{
			curTime = new Date();
			return String(curTime.getTime());
		}
		
		
	}
	
}
